package cn.diancan.action.manager;

import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;

import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.ShopClerk;
import cn.diancan.vo.manager.ShopClerkManagerActionVo;
import cn.diancan.vo.other.Pager;

/**
 * 营业员
 * @author Administrator
 */
public class ShopClerkAction extends BaseAction implements ModelDriven<ShopClerkManagerActionVo>{
    
    private ShopClerkManagerActionVo requestVo = new ShopClerkManagerActionVo(); 
    private Pager pager = new Pager();
    
    //List
    @ManagerAuth
    public String shopClerkListPage(){
	try {
	    pager.setPageNumber(requestVo.getPageNumber());
	    pager.setPageSize(10);
	    shopClerkService.findByPager(pager,requestVo.getJobNumber(),requestVo.getClerkName());
	    this.put("pager", pager);
	    this.put("requestVo", requestVo);

	    return freemarkView("/views/ftl/manager/shopclerk/shop_clerk_list.ftl");
	} catch (Exception e) {
	    return managerError(e.getMessage());
	}
    }
    
    
    @ManagerAuth
    public String createShopClerkPage(){ 

	return freemarkView("/views/ftl/manager/shopclerk/create_shop_clerk.ftl");
    }
    
    
    @ManagerAuth
    public String createShopClerk(){
	try {
	    ShopClerk shopClerk= shopClerkService.saveShopClerk(requestVo.getJobNumber(), requestVo.getClerkName(), requestVo.getRemark());
	    JSONObject jsonObject = new JSONObject();
	    jsonObject.put("shopClerkId",shopClerk.getId());
	    jsonObject.put("jobNumber", shopClerk.getJobNumber());
	    jsonObject.put("clerkName", shopClerk.getClerkName());
	    
	    return jsonFormat(jsonObject,"添加成功","添加失败");
	} catch (OutMsgException e) {
	    return jsonError(e);
	} catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
    
    
    @ManagerAuth
    public String shopClerkInfo(){
	try {
	    ShopClerk shopClerk= shopClerkService.findByShopClerkId(requestVo.getShopClerkId());
	    this.put("shopClerk",shopClerk);

	    return freemarkView("/views/ftl/manager/shopclerk/shop_clerk_info.ftl");
	} catch (Exception e) { 
	    return managerError(e.getMessage());
	}
    }
    
    
    @ManagerAuth
    public String modifyShopClerk(){
	try {
	    Boolean result= shopClerkService.updateShopClerk(requestVo.getShopClerkId(), requestVo.getJobNumber(), requestVo.getClerkName(), requestVo.getRemark());
	    
	    return jsonFormat(result,"修改成功","修改失败");
	} catch (OutMsgException e) {
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
    
    
    @ManagerAuth
    public String removeShopClerk(){
	try {
	    Boolean result = shopClerkService.deleteShopClerk(requestVo.getShopClerkId());
	    return jsonFormat(result,"删除成功","删除失败");
	} catch (OutMsgException e) { 
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
    
    
    @ManagerAuth
    public String shopClerkList(){
	try {
	    List<ShopClerk> list = shopClerkService.findList();
	    return jsonFormat(list);
	} catch (OutMsgException e) { 
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
    


    @Override
    public ShopClerkManagerActionVo getModel() { 
	return requestVo;
    }
    

}
